package com.ourcraft.informationcollectionsystem.mapper;

import com.github.yulichang.base.MPJBaseMapper;
import com.ourcraft.informationcollectionsystem.entity.CompanyDataReportForm;
import com.ourcraft.informationcollectionsystem.entity.DTO.UserDetailInfo;
import com.ourcraft.informationcollectionsystem.entity.DataReportForm;
import com.ourcraft.informationcollectionsystem.entity.DataReportFormDetail;
import org.apache.ibatis.annotations.*;

import java.sql.Date;
import java.util.List;

@Mapper
public interface DataReportFormMapper extends MPJBaseMapper<DataReportForm>{

    @Select("select d.form_id as formId, d.form_start_total_num as formStartTotalNum, " +
            "d.form_end_total_num as formEndTotalNum, d.form_decrease_type as formDecreaseType, " +
            "d.form_first_reason as formFirstReason, d.form_second_reason as formSecondReason, " +
            "d.form_third_reason as formThirdReason, d.form_reason_description as formReasonDescription, " +
            "d.form_examine_status as formExamineStatus, c.company_chinese_name as companyChineseName, " +
            "c.company_city as companyCity, i.investigation_title as investigationTitle, " +
            "h.history_date as lastSubmitDate " +
            "from DataReportForm d " +
            "left join (select h1.form_id as form_id, max(h1.history_date) as history_date from DataReportFormHistory h1 where h1.history_type = 0 group by h1.form_id) h on d.form_id = h.form_id " +
            "join Company c on d.company_id = c.company_id " +
            "join InvestigationPeriod i on d.investigation_id = i.investigation_id;")
    @Results({
            @Result(column = "formId", property = "formId"),
            @Result(column = "formStartTotalNum", property = "formStartTotalNum"),
            @Result(column = "formEndTotalNum", property = "formEndTotalNum"),
            @Result(column = "formDecreaseType", property = "formDecreaseType"),
            @Result(column = "formFirstReason", property = "formFirstReason"),
            @Result(column = "formSecondReason", property = "formSecondReason"),
            @Result(column = "formThirdReason", property = "formThirdReason"),
            @Result(column = "formReasonDescription", property = "formReasonDescription"),
            @Result(column = "formExamineStatus", property = "formExamineStatus"),
            @Result(column = "companyChineseName", property = "companyChineseName"),
            @Result(column = "companyCity", property = "companyCity"),
            @Result(column = "investigationTitle", property = "investigationTitle"),
            @Result(column = "lastSubmitDate", property = "lastSubmitDate"),
    })
    List<DataReportFormDetail> selectDataReportFormDetails();


    @Select("select d.form_id as formId, c.user_id as userId, " +
            "d.form_decrease_type as formDecreaseType, d.form_start_total_num as formStartTotalNum, " +
            "d.form_end_total_num as formEndTotalNum, d.form_first_reason as formFirstReason, " +
            "d.form_examine_status as formExamineStatus," +
            "i.investigation_start_date as formStartDate, " +
            "i.investigation_length as investigationLength " +
            "from DataReportForm d, InvestigationPeriod i, Company c " +
            "where d.investigation_id = i.investigation_id and d.company_id = c.company_id;")
    @Results({
            @Result(column = "formId", property = "formId"),
            @Result(column = "userId", property = "userId"),
            @Result(column = "formDecreaseType", property = "formDecreaseType"),
            @Result(column = "formStartTotalNum", property = "formStartTotalNum"),
            @Result(column = "formEndTotalNum", property = "formEndTotalNum"),
            @Result(column = "formStartDate", property = "formStartDate"),
            @Result(column = "investigationLength", property = "investigationLength"),
            @Result(column = "formFirstReason", property = "formFirstReason"),
            @Result(column = "formExamineStatus", property = "formExamineStatus")
    })
    List<CompanyDataReportForm> selectCompanyDataReportForm();


    // formID , InvestigationTitle , investigationID , formStartDate  ,
    // formEndDate  , investigationDescription , formSubmitDate, formExamineStatus
    @Select("select d.form_id as formId, d.company_id as companyId, " +
            "d.investigation_id as investigationId," +
            "d.form_decrease_type as formDecreaseType, d.form_start_total_num as formStartTotalNum, " +
            "d.form_end_total_num as formEndTotalNum, d.form_first_reason as formFirstReason, " +
            "d.form_second_reason as formSecondReason, d.form_third_reason as formThirdReason, " +
            "d.form_examine_status as formExamineStatus " +
            "from DataReportForm d, InvestigationPeriod i " +
            "where d.investigation_id = i.investigation_id  AND d.company_id = #{companyId} AND i.investigation_start_date <= #{currentDate};")
    @Results({
            @Result(column = "formId", property = "formId"),
            @Result(column = "companyId", property = "companyId"),
            @Result(column = "investigationId", property = "investigationId"),
            @Result(column = "formDecreaseType", property = "formDecreaseType"),
            @Result(column = "formStartTotalNum", property = "formStartTotalNum"),
            @Result(column = "formEndTotalNum", property = "formEndTotalNum"),
            @Result(column = "formFirstReason", property = "formFirstReason"),
            @Result(column = "formSecondReason", property = "formSecondReason"),
            @Result(column = "formThirdReason", property = "formThirdReason"),
            @Result(column = "formExamineStatus", property = "formExamineStatus")
    })
    List<DataReportForm> selectNeedCompanyDataReportForm(@Param("companyId") Integer companyId, @Param("currentDate") Long currentDate);

    @Update("update DataReportForm set form_examine_status = 7 where form_examine_status = 6 and investigation_id = #{investigationId}")
    void updateDataReportFormStatus(@Param("investigationId") Integer investigationId);
}
